
#ifndef _ACTIVITY_TIME_CONFIG_H_
#define _ACTIVITY_TIME_CONFIG_H_

#include <unordered_map>
#include <vector>
#include "base.h"

#include "../../3rd/libjson/json.h"

class CTimeSpace
{
public:
	CTimeSpace();
	~CTimeSpace();

	int32_t begin_time() const { return begin_time_; }
	void set_begin_time(int32_t begin_time) { begin_time_ = begin_time; }

	int32_t end_time() const { return end_time_; }
	void set_end_time(int32_t end_time) { end_time_ = end_time; }

	int32_t week_day() const { return week_day_; }
	void set_week_day(int32_t week_day) { week_day_ = week_day; }
private:
	int32_t week_day_;
	int32_t begin_time_;
	int32_t end_time_;
};

typedef std::vector< CTimeSpace > TTimeSpaceList;
typedef std::vector< int32_t > TWeekDayVector;
typedef std::vector< int32_t > TDateVector;
class CActivityTpl
{
public:
	CActivityTpl();

	~CActivityTpl();

	bool LoadFromJsonNode(const Json::Value& activity_json);

	int32_t activity_id() const { return activity_id_; }

	const TTimeSpaceList& time_space_list() const { return time_space_list_; }

	const TWeekDayVector& weekdays() const { return weekdays_; }

	int32_t cycle() const { return cycle_; }
	int32_t cycle_begin_time() const { return cycle_begin_time_; }
	const std::vector< int32_t >& cycle_days() const { return cycle_days_; }

	bool IsOpen(tm& t) const;
	bool CanJoinInActivity(tm& t) const;

	// 获取now所在周期的第一个开始时间,如找不到返回0;
	int32_t GetNowCycleBeginTime0(int32_t now) const;
	// 获取now下个周期的第一个开始时间,如找不到返回0;
	int32_t GetNextCycleBeginTime0(int32_t now) const;
	int32_t GetNextFireTime() const;
	int32_t join_time()const { return join_time_; }
	int32_t minlimit()const { return minlimit_lv_; }
	int32_t maxlimit()const { return maxlimit_lv_; }
private:

	int32_t activity_id_;
	int32_t type_start_;
	int32_t join_time_;		//参加次数
	int32_t maxlimit_lv_;	//最大等级限制
	int32_t minlimit_lv_;	//最小等级限制

	TTimeSpaceList time_space_list_;
	TWeekDayVector weekdays_;
	TDateVector dates_;

	int32_t cycle_;/**<周期  */
	int32_t cycle_begin_time_;/**<周期开始时间  */
	std::vector< int32_t > cycle_days_;/**<周期日  */
};

typedef std::vector< CActivityTpl > TDailyActivityList;

class CActivityConfig
{
public:
	typedef std::unordered_map< int32_t, CActivityTpl > TActivityMap;

	CActivityConfig();

	~CActivityConfig();

	bool LoadFromFile();

	const CActivityTpl* GetActivityByID(int32_t activity_id) const;
	const TActivityMap& activity_map() const { return activity_map_; }
	static int32_t GetNowWeekTime(int32_t now, int32_t weekday, int32_t hour, int32_t min);
	static int32_t GetNowCycleTime(int32_t now, int32_t cycle_begin_time, int32_t cycle, int32_t cycle_day, int32_t hour, int32_t min);
	static int32_t GetNextCycleTime(int32_t now, int32_t cycle_begin_time, int32_t cycle, int32_t cycle_day, int32_t hour, int32_t min);
private:
	TActivityMap activity_map_;
};


#endif

